package org.refresh.emf;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.refresh.core.intelligence.Constraint;
import org.refresh.core.intelligence.ObjectType;
import org.refresh.core.intelligence.RefreshKB;
import org.refresh.core.intelligence.emf.OCLConstraintEvaluator;

/*******************************************************************************
 * Copyright (c) 2005, 2006 Jules White. All rights reserved. This program and
 * the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
public class EMFIntelligence extends RefreshKB {

	public static final String OCL_TYPE = "ocl";

	public EMFIntelligence() {
		OCLConstraintEvaluator eval = new OCLConstraintEvaluator();
		getConstraintEvaluators().put(OCL_TYPE, eval);
		getObjectViews().add(new EMFView());
	}

	public void add(EClass c, EReference feat, Constraint con) {
		EObjectType t = EObjectType.getType(c);
		EObjectType t2 = EObjectType.getType(feat.getEReferenceType());
		super.add(t, feat, t2, con);
	}

	public void add(EClass c, Object feat, Constraint con) {
		add(c, (EReference) feat, con);
	}

	public void add(EClass c, Object rel, EClass target, Constraint con) {
		EObjectType t = EObjectType.getType(c);
		EObjectType t2 = EObjectType.getType(target);
		super.add(t, rel, t2, con);
	}

	public void add(EObject obj) {
		EObjectType t = EObjectType.getType(obj.eClass());
		installType(t);
		super.add(obj);
	}

	protected void installType(EObjectType type) {
		if (!getObjectTypes().contains(type)) {
			getObjectTypes().add(type);
			for (ObjectType t : type.getSuperTypes()) {
				if (!getObjectTypes().contains(t)) {
					getObjectTypes().add(t);
				}
			}
		}
	}

}
